package nsu.edu.zsq.service.impl.sys;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;
import nsu.edu.zsq.bean.CourseExample;
import nsu.edu.zsq.bean.SchMajor;
import nsu.edu.zsq.bean.SchMajorExample;
import nsu.edu.zsq.common.RequestHolder;
import nsu.edu.zsq.dao.CourseMapper;
import nsu.edu.zsq.dao.SchMajorMapper;
import nsu.edu.zsq.exception.ParamException;
import nsu.edu.zsq.param.MajorParam;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.service.sys.SchMajorService;
import nsu.edu.zsq.service.sys.SysLogService;
import nsu.edu.zsq.util.BeanValidator;
import nsu.edu.zsq.util.ExcelUtil;

/** 
* Description: 学校专业的service实现类<br>
*/
@Slf4j
@Service
public class SchMajorServiceImpl implements SchMajorService{

    @Autowired
    private SchMajorMapper schMajorMapper;
    @Autowired
    private SysLogService sysLogService;
    @Autowired
    private CourseMapper courseMapper;
    
    @Override
    public SchMajor getSchMajor(int id) {
        SchMajor schMajor = schMajorMapper.selectByPrimaryKey(id);
        return schMajor;
    }
    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveSchMajor(SchMajor schMajor) {
        // 参数校验
        BeanValidator.check(schMajor);
		/*
		 * schMajor.setOperator(RequestHolder.getCurrentSysUser().getAccount());
		 * schMajor.setOperatorTime(new Date());
		 */
        if (checkMajorNoExist(schMajor.getMajorNo())) {
            throw new ParamException("该专业代码已存在");
        }
        // 1.添加专业
        schMajorMapper.insertSelective(schMajor);
        
		/*
		 * // 2.添加日志 String instruction = "专业管理-添加专业";
		 * sysLogService.saveSysLogBySysUser(null, schMajor, instruction);
		 */
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveBatchSchMajor(List<SchMajor> listSchMajor) {
        for (SchMajor schMajor : listSchMajor) {
			/*
			 * schMajor.setOperator(RequestHolder.getCurrentSysUser().getAccount());
			 * schMajor.setOperatorTime(new Date());
			 */
            if (checkMajorNoExist(schMajor.getMajorNo())) {
                throw new ParamException("存在相同的专业代码");
            }
            // 1.添加专业
            schMajorMapper.insertSelective(schMajor);
            
        }
        
		/*
		 * // 2.添加日志 String instruction = "专业管理-批量导入";
		 * sysLogService.saveSysLogBySysUser(null, listSchMajor, instruction);
		 */
        
    }
    
    /**
     * 通过专业代码，检查数据库是否存在该专业
     * @param majorNo 专业代码
     * @return boolean true:存在，false：不存在
     */
    private boolean checkMajorNoExist(String majorNo) {
        return schMajorMapper.countByMajorNo(majorNo) > 0;
    }

    @Override
    public PageInfo<SchMajor> listSchMajorWithPage(int orgId, PageQuery pageQuery) {
        // 所有的专业
        int allSchMajor = 0;
        PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        SchMajorExample example = new SchMajorExample();
        List<SchMajor> listSchMajor = null;
        if (orgId == allSchMajor) {
            listSchMajor = schMajorMapper.selectByExample(example);
        } else {
            example.createCriteria().andOrgIdEqualTo(orgId);
            listSchMajor = schMajorMapper.selectByExample(example);
        }
        PageInfo<SchMajor> pageInfo = new PageInfo<SchMajor>(listSchMajor, pageQuery.getNavigatePages());
        return pageInfo;
    }
    
    @Override
    public List<SchMajor> listSchMajor() {
        SchMajorExample example = new SchMajorExample();
        List<SchMajor> listSchMajor = schMajorMapper.selectByExample(example);
        return listSchMajor;
    }

    
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBatchSchMajor(int[] majorIds) {
        List<SchMajor> before = schMajorMapper.listSchMajorByMajorIds(majorIds);
        for (SchMajor schMajor : before) {
            if (checkCourseExistByMajorNo(schMajor.getMajorNo())) {
                throw new ParamException("所选专业下面存在课程，无法删除");
            }
            // 删除专业
            schMajorMapper.deleteByPrimaryKey(schMajor.getId());
        }

		/*
		 * // 添加日志 String instruction = "专业管理-批量删除";
		 * sysLogService.saveSysLogBySysUser(before, null, instruction);
		 */
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSchMajor(int majorId) {
        SchMajor before = schMajorMapper.selectByPrimaryKey(majorId);
        if (checkCourseExistByMajorNo(before.getMajorNo())) {
            throw new ParamException("该专业下面存在课程，无法删除");
        }
        // 删除
        schMajorMapper.deleteByPrimaryKey(majorId);
        
		/*
		 * // 添加日志 String instruction = "专业管理-单个删除";
		 * sysLogService.saveSysLogBySysUser(before, null, instruction);
		 */
    }
    
    /**
     * 通过专业代码检查该专业下面是否存在课程
     * @param majorNo 专业代码
     * @return boolean true：存在 false：不存在
     */
    private boolean checkCourseExistByMajorNo(String majorNo) {
        CourseExample example = new CourseExample();
        example.createCriteria().andMajorNoEqualTo(majorNo);
        return courseMapper.countByExample(example) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSchMajor(SchMajor schMajor) {
        // 参数校验
        BeanValidator.check(schMajor);
        SchMajor before = getSchMajor(schMajor.getId());
        if (!before.getMajorNo().equals(schMajor.getMajorNo())) {
            if (checkMajorNoExist(schMajor.getMajorNo())) {
                throw new ParamException("存在相同的专业代码");
            }
        }
		/*
		 * schMajor.setOperator(RequestHolder.getCurrentSysUser().getAccount());
		 * schMajor.setOperatorTime(new Date());
		 */
        schMajorMapper.updateByPrimaryKeySelective(schMajor);
        
		/*
		 * // 添加日志 SchMajor after = new SchMajor(); BeanUtils.copyProperties(before,
		 * after); after.setMajorName(schMajor.getMajorName());
		 * after.setMajorNo(schMajor.getMajorNo());
		 * after.setOperator(schMajor.getOperator());
		 * after.setOperatorTime(schMajor.getOperatorTime()); String instruction =
		 * "专业管理-编辑"; sysLogService.saveSysLogBySysUser(before, after, instruction);
		 */
    }


}
